package com.spreedairy.product;

import java.io.IOException;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
import com.liferay.portal.kernel.dao.orm.OrderFactoryUtil;
import com.liferay.portal.kernel.dao.search.SearchContainer;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portlet.shopping.model.ShoppingCart;
import com.liferay.portlet.shopping.model.ShoppingItem;
import com.liferay.portlet.shopping.service.ShoppingCartLocalServiceUtil;
import com.liferay.portlet.shopping.service.ShoppingItemLocalServiceUtil;
import com.spreedairy.common.CommonAction;
import com.spreedairy.util.ProductUtil;

/**
 * Portlet implementation class ProductPortlet
 */
public class ProductPortlet extends CommonAction {
 
	
	public void doView(RenderRequest renderRequest,
			RenderResponse renderResponse) throws IOException, PortletException {
		List<ShoppingItem> results = null;
		SearchContainer searchContainer = null;
		if(Validator.isNotNull(renderRequest.getAttribute("searchContainer"))){
			searchContainer = (SearchContainer)renderRequest.getAttribute("searchContainer");
		}
		DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(ShoppingItem.class, PortalClassLoaderUtil.getClassLoader());
		dynamicQuery.addOrder(OrderFactoryUtil.desc("createDate"));
		try {
			results = ShoppingItemLocalServiceUtil.dynamicQuery(dynamicQuery);
		} catch (SystemException e) {
			e.printStackTrace();
		}
		if(Validator.isNull(searchContainer)) {
			PortletURL portletURL = renderResponse.createRenderURL();
			searchContainer = new SearchContainer(renderRequest,
					null, null, "cur_content", 6, portletURL, null, StringPool.BLANK);
			searchContainer.setTotal(results.size());
		}
		int start = searchContainer.getStart();
		int end = searchContainer.getEnd();
		if(results.size() < searchContainer.getEnd()) {
			end = results.size();
		}
		results  = results.subList(start, end);
		searchContainer.setResults(results);
		renderRequest.setAttribute("searchContainer",
				searchContainer);
		renderRequest.setAttribute("ITEM_LIST", results);
		
		super.doView(renderRequest, renderResponse);
	}
	
	public void userWishItem(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {
		addUserWish(actionRequest, actionResponse);
	}
	
	public void updateCartlist(
			ActionRequest actionRequest, ActionResponse actionResponse)
		throws Exception {

		String cmd = ParamUtil.getString(actionRequest, Constants.CMD);

		ShoppingCart cart = ProductUtil.getCart(actionRequest);

		if (cmd.equals(Constants.ADD)) {
			long itemId = ParamUtil.getLong(actionRequest, "itemId");

			String fields = ParamUtil.getString(actionRequest, "fields");

			if (Validator.isNotNull(fields)) {
				fields = "|" + fields;
			}

			ShoppingItem item = ShoppingItemLocalServiceUtil.getItem(itemId);

			if (item.getMinQuantity() > 0) {
				for (int i = 0; i < item.getMinQuantity(); i++) {
					cart.addItemId(itemId, fields);
				}
			}
			else {
				cart.addItemId(itemId, fields);
			}
		}
		else {
			String itemIds = ParamUtil.getString(actionRequest, "itemIds");
			String couponCodes = ParamUtil.getString(
				actionRequest, "couponCodes");
			int altShipping = ParamUtil.getInteger(
				actionRequest, "altShipping");
			boolean insure = ParamUtil.getBoolean(actionRequest, "insure");

			cart.setItemIds(itemIds);
			cart.setCouponCodes(couponCodes);
			cart.setAltShipping(altShipping);
			cart.setInsure(insure);
		}

		ShoppingCartLocalServiceUtil.updateCart(
			cart.getUserId(), cart.getGroupId(), cart.getItemIds(),
			cart.getCouponCodes(), cart.getAltShipping(), cart.isInsure());

		if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
			addSuccessMessage(actionRequest, actionResponse);
		}
		 
		 
	/*	String redirect = ParamUtil.getString(actionRequest, "redirect");

			String cartvalue="CartPage";
			PortletSession session = actionRequest.getPortletSession();
			session.setAttribute("sessionValue",cartvalue,PortletSession.APPLICATION_SCOPE);
			System.out.println("redirect"+redirect);
			actionResponse.setRenderParameter("jspPage","/html/portlet/shopping/view_item.jsp");*/
	
		
	}

}
